package com.client.view;

import com.client.bean.UserInfo;
import com.client.common.CmdControl;
import com.client.common.SendMsg;
import com.client.common.SendServer;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/12/18.
 */
public class ChatView extends JFrame implements ActionListener , ItemListener{
	private UserInfo selfUser , friendUser;

	JTextPane recePanel , sendPanel;
	JButton btnsend , btnshake , /*btnsendFile ,*/ btnColor , btnClose;
	JComboBox cbFont,cbSize , cbImg;
	JLabel lblboy , lblgirl , lblfriendInfo;

	public ChatView(){

	}

	public ChatView(UserInfo selfUser , UserInfo friendUser) {
		this.selfUser = selfUser;
		this.friendUser = friendUser;
		ImageIcon ic = new ImageIcon(friendUser.getFace());
		setIconImage(ic.getImage());
		String str = "";
		if (friendUser.getRemark() != null && !friendUser.getRemark().equals("")) {
			str = friendUser.getRemark() + "(";
		} else {
			str = friendUser.getNicknName() + "(";
		}
		str += friendUser.getCode() + ")," + friendUser.getInformation();
		setTitle(str);

		lblfriendInfo = new JLabel(str , ic , JLabel.LEFT);
		add(lblfriendInfo , BorderLayout.NORTH);
		recePanel = new JTextPane();
		recePanel.setEditable(false);
//		add(new JScrollPane(recePanel));

		btnshake = new JButton(new ImageIcon("face/zd.png"));
		btnshake.setMargin((new Insets(0,0,0,0)));//按钮的大小

//		btnsendFile = new JButton("发送文件");

		btnColor = new JButton("设置字体颜色");

		String[] fonts = {"宋体" , "楷体" , "黑体" , "隶书" , "华文新魏"};
		cbFont = new JComboBox(fonts);
		String fontsize[] = {"10" , "11" , "12" , "13" , "14" , "18" , "24"};
		cbSize = new JComboBox(fontsize);
		cbImg = new JComboBox(getImg());

		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		btnPanel.add(cbFont);
		btnPanel.add(cbSize);
		btnPanel.add(btnColor);
		btnPanel.add(btnshake);
//		btnPanel.add(btnsendFile);
		btnPanel.add(cbImg);
		sendPanel = new JTextPane();
		JPanel southPanel = new JPanel(new BorderLayout());
		southPanel.add(btnPanel , BorderLayout.NORTH);
		southPanel.add(new JScrollPane(sendPanel));

//		add(southPanel , BorderLayout.SOUTH);

		JPanel centerPanel = new JPanel(new GridLayout(2,1,5,5));
		centerPanel.add(new JScrollPane(recePanel));
		centerPanel.add(new JScrollPane(southPanel));
		add(centerPanel);
//		add(new JScrollPane(recePanel));

//		add(southPanel);

		btnsend = new JButton("发送(S)");
		btnsend.setMnemonic('S');
		btnClose = new JButton("关闭(X)");
		btnClose.setMnemonic('X');
		JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		bottomPanel.add(btnsend);
		bottomPanel.add(btnClose);
		add(bottomPanel , BorderLayout.SOUTH);

		lblboy = new JLabel(new ImageIcon("images/advertisement.png"));
		lblgirl = new JLabel(new ImageIcon("images/advertisement.png"));

		JPanel rightPanel = new JPanel(new GridLayout(2,1,5,0));
		rightPanel.add(lblboy);
		rightPanel.add(lblgirl);
		add(rightPanel , BorderLayout.EAST);

		btnshake.addActionListener(this);
		btnColor.addActionListener(this);
		btnsend.addActionListener(this);
		btnClose.addActionListener(this);
//		btnsendFile.addActionListener(this);
		cbFont.addItemListener(this);
		cbSize.addItemListener(this);
		cbImg.addItemListener(this);

		setSize(600 , 500);
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//窗口不关闭，只隐藏

		sendPanel.requestFocus();
	}

	public Icon[] getImg() {
		Icon[] icons = null;
		File file = new File("expression");
		String sfile[] = file.list();

		icons = new ImageIcon[sfile.length];

		for (int i = 0; i < sfile.length; i++) {
			icons[i] = new ImageIcon("expression/" + sfile[i]);
		}
		return icons;
	}

	public static void main(String[] args) {
//		new ChatView().getImg();
	}

	//将发送框的内容提交到接收框 ， 同时清除发送框的内容
	public void appendView(String name , StyledDocument xx) throws BadLocationException {
		StyledDocument vdoc = recePanel.getStyledDocument();

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = sdf.format(date);

		SimpleAttributeSet as = new SimpleAttributeSet();

		String s = name + "   " + time + "\n";
		vdoc.insertString(vdoc.getLength() , s ,as);

		int end = 0;

		//处理显示内容
		while (end < xx.getLength()) {
			Element e0 = xx.getCharacterElement(end);

			//获取元素对应的风格
			SimpleAttributeSet as1 = new SimpleAttributeSet();
			StyleConstants.setForeground(as1 , StyleConstants.getForeground(e0.getAttributes()));
			StyleConstants.setFontSize(as1 , StyleConstants.getFontSize(e0.getAttributes()));
			StyleConstants.setFontFamily(as1 , StyleConstants.getFontFamily(e0.getAttributes()));

			s = e0.getDocument().getText(end , e0.getEndOffset() - end);

			if ("icon".equals(e0.getName())) {
				vdoc.insertString(vdoc.getLength() , s , e0.getAttributes());
			} else {
				vdoc.insertString(vdoc.getLength() , s , as1);
			}
			end = e0.getEndOffset();

			//换行
			vdoc.insertString(vdoc.getLength() , "\n" , as);

			//设置显示视图和字符的位置与文档结尾 ，以便视图滚动
			recePanel.setCaretPosition(vdoc.getLength());
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnsend) { //发送信息
			try {
				appendView(selfUser.getNicknName() , sendPanel.getStyledDocument());
				/**
				 * 发送消息
				 */
				//生成发送类
				SendMsg msg = new SendMsg();
				msg.Cmd = CmdControl.CMD_CHAT;
				msg.doc = sendPanel.getStyledDocument();//聊天内容
				msg.selfUserInfo = selfUser;
				msg.friendUserInfo = friendUser;

				//发送
				new SendServer().send(msg);

				sendPanel.setText("");
			} catch (BadLocationException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == btnshake) {//抖动
			SendMsg msg = new SendMsg();
			msg.Cmd = CmdControl.CMD_DOUDONG;
			msg.selfUserInfo = selfUser;
			msg.friendUserInfo = friendUser;
			new SendServer().send(msg);

			shake();

//			Point p = this.getLocationOnScreen();//得到当前界面坐标
//			for (int i = 0; i < 20; i++) {
//				if (i%2 == 0) {
//					setLocation(p.x + 5 , p.y + 5);
//				} else {
//					setLocation(p.x - 5 , p.y - 5);
//				}
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e1) {
//					e1.printStackTrace();
//				}
//			}

		} else if (e.getSource() == btnColor) {
			JColorChooser cc = new JColorChooser();
			Color c = cc.showDialog(this , "选择字体" , Color.BLACK);
			sendPanel.setForeground(c);
//		} else if (e.getSource() == btnsendFile) {
//			//创建文件选择框
//			FileDialog fileDialog = new FileDialog(this , "传送64k以下文件" , FileDialog.LOAD);
//			fileDialog.show();
//			String sfilename = fileDialog.getDirectory() + "\\" + fileDialog.getFile();
//		} else if (e.getSource() == btnClose) {
			dispose();
		}
	}

	public void shake() {
		Point p = this.getLocationOnScreen();//得到当前界面坐标
		for (int i = 0; i < 20; i++) {
			if (i%2 == 0) {
				setLocation(p.x + 5 , p.y + 5);
			} else {
				setLocation(p.x - 5 , p.y - 5);
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == cbFont || e.getSource() == cbSize) {
			String sfont = cbFont.getSelectedItem().toString();
			int size = Integer.parseInt(cbSize.getSelectedItem().toString());
			sendPanel.setFont(new Font(sfont ,  Font.PLAIN , size));
		} else if (e.getSource() == cbImg) {
			Icon g = (Icon) cbImg.getSelectedItem();
			sendPanel.insertIcon(g);
		}
	}
}
